package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.QiYeZhongXinActivity;
import com.tangkun.environment.entity.gongzuo.EnterpriseQueryReturn;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.utils.SharePrefercesUtils;
import com.tangkun.environment.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: WuRanYuanAdapter
 * @Description: 搜索污染源列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class SearchWuRanYuanAdapter extends BaseAdapter {

    private Context context;
    private List<EnterpriseQueryReturn.DataBean> dataList = new ArrayList<EnterpriseQueryReturn.DataBean>();

    public SearchWuRanYuanAdapter(Context context, List<EnterpriseQueryReturn.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_wuranyuan, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            //tangkun 暂时隐藏吧 污染源认证
//            if (dataList.get(position).sex) {
//                holder.ivWuranyuanWuranyuanrenzheng.setVisibility(View.VISIBLE);
//            } else {
            holder.ivWuranyuanWuranyuanrenzheng.setVisibility(View.INVISIBLE);
//            }
            //图片
            Glide.with(context)
                    .load(GlobalUrl.IMG_URL + dataList.get(position).img)
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_place_holder)
                    .into(holder.ivWuranyuanImg);
            //企业名称
            if (!TextUtils.isEmpty(dataList.get(position).name)) {
                holder.tvWuranyuanContent.setText(dataList.get(position).name);
            } else {
                holder.tvWuranyuanContent.setText("");
            }
            //地址
            if (!TextUtils.isEmpty(dataList.get(position).address)) {
                holder.tvWuranyuanDizhi.setText(dataList.get(position).address);
            } else {
                holder.tvWuranyuanDizhi.setText("");
            }
            try {
                String lat = dataList.get(position).lat;
                String lng = dataList.get(position).lng;
                String myLat = SharePrefercesUtils.getLatitude(context);
                String myLng = SharePrefercesUtils.getLongitude(context);
                LatLng latLngStart = new LatLng(Double.valueOf(lat).doubleValue(), Double.valueOf(lng).doubleValue());
                LatLng latLngEnd = new LatLng(Double.valueOf(myLat).doubleValue(), Double.valueOf(myLng).doubleValue());
                //计算p1、p2两点之间的直线距离，单位：米
                String distance = "";
                if (DistanceUtil.getDistance(latLngStart, latLngEnd) > 1000) {
                    distance = Utils.getOneDecimalPoint(DistanceUtil.getDistance(latLngStart, latLngEnd) / 1000) + "km";
                } else {
                    distance = Utils.getOneDecimalPoint(DistanceUtil.getDistance(latLngStart, latLngEnd)) + "m";
                }
                //距离
                holder.tvWuranyuanJuli.setText(distance);
            } catch (Exception e) {
                //距离
                holder.tvWuranyuanJuli.setText("");
            }

            //跳转基础资料页面
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), QiYeZhongXinActivity.class);
                    i.putExtra("id", dataList.get(position).ids);
                    i.putExtra("name", dataList.get(position).name);
                    v.getContext().startActivity(i);
                }
            });
        }
        return convertView;

    }

    /**
     * 计算两点之间距离
     *
     * @param start
     * @param end
     * @return 米
     */
    public String getDistance(LatLng start, LatLng end) {
        double lat1 = (Math.PI / 180) * start.latitude;
        double lat2 = (Math.PI / 180) * end.latitude;

        double lon1 = (Math.PI / 180) * start.longitude;
        double lon2 = (Math.PI / 180) * end.longitude;

        //地球半径
        double R = 6371;

        //两点间距离 km，如果想要米的话，结果*1000
        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
        if (d < 1)
            return (int) d * 1000 + "m";
        else
            return String.format("%.2f", d) + "km";
    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.iv_wuranyuan_img)
        ImageView ivWuranyuanImg;
        @BindView(R.id.iv_wuranyuan_wuranyuanrenzheng)
        ImageView ivWuranyuanWuranyuanrenzheng;
        @BindView(R.id.tv_wuranyuan_content)
        TextView tvWuranyuanContent;
        @BindView(R.id.tv_wuranyuan_dizhi)
        TextView tvWuranyuanDizhi;
        @BindView(R.id.tv_wuranyuan_juli)
        TextView tvWuranyuanJuli;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
